package com.hit.userManageService.controller;


import com.hit.JwtUtils;
import com.hit.commonUtils.R;
import com.hit.userManageService.entity.UcenterMember;
import com.hit.userManageService.entity.vo.RegisterVo;
import com.hit.userManageService.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-11-14
 */
@RestController
@CrossOrigin
@RequestMapping("/userManageService/ucenter-member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;

    //1.注册
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.registerUser(registerVo);
        return R.ok();
    }

    //2.登陆
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member){
        //调用service服务实现登陆
        //返回token值，使用jwt生成
        String token=ucenterMemberService.login(member);
        return R.ok().data("token",token);

    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        return R.ok().data("userInfo",member);

    }

    //根据id获取用户信息,如果使用上面的getMemberInfo（参数是HttpServletRequest对象），直接用feign调用会报错
    @GetMapping("getInfoUc/{id}")
    public UcenterMember getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        return ucenterMember;
    }

}

